package com.fx.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;


import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.vod.Utils.ConstantVodUtils;
import com.fx.vod.Utils.InitVodCilent;
import com.fx.vod.service.VodService;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
public class VodController {


    final private VodService vodService;

    //上传视频到阿里云
    @PostMapping("/uploadAlyiVideo")
    public ReturnMessage<Object> uploadAlyiVideo(MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideoAly(file);
        return ReturnMessageUtils.success("vodeId:",videoId);
    }

    //根据视频id删除阿里云视频
    @DeleteMapping("/removeAlyVideo/{id}")
    public ReturnMessage<Object> removeAlyVideo(@PathVariable String id) {

            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
        try {
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return ReturnMessageUtils.success();
    }


    //删除多个阿里云视频的方法
    //参数多个视频id  List videoIdList
    @DeleteMapping("/delete-batch")
    public ReturnMessage<Object> deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        return ReturnMessageUtils.success();
    }


    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public ReturnMessage<Object> getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return ReturnMessageUtils.success("playAuth",playAuth);
        }catch(Exception e) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_ACTIVIT_DEFINITION_GET);
        }
    }










}
